#ifndef _HARDWIRE_H_
#define _HARDWIRE_H_

#include "polsys_mcu.h"

#define TSS_DEBUG

// SPI 管脚定义。SPI管脚初始化在SPI外设初始化中进行。

#define SPI_MISO_PIN            GPIO_P33
#define SPI_MOSI_PIN            GPIO_P34
#define SPI_SCK_PIN             GPIO_P00
#define SPI_LCD_CS_PIN          GPIO_P27
#define SPI_FLASH_CS_PIN        GPIO_P26

#define VEXT_EN_PIN             GPIO_P31

#define VBUS_HZ_PIN             GPIO_P32

#define LCD_RS_PIN              GPIO_P01

#define LCD_BR_PIN              GPIO_P18

#define VBUS_TX_PIN             GPIO_P09
#define VBUS_TX_PIN_GPIO_OUT_L  {HalGpioFmuxEnable(VBUS_TX_PIN, Bit_DISABLE);HalGpioSet(VBUS_TX_PIN, (bit_action_e)0);}
#define VBUS_TX_PIN_GPIO_OUT_H  {HalGpioFmuxEnable(VBUS_TX_PIN, Bit_DISABLE);HalGpioSet(VBUS_TX_PIN, (bit_action_e)1);}
#define VBUS_TX_PIN_UART1_TX    HalGpioFmuxConfig(VBUS_TX_PIN, FMUX_UART1_TX)

#define VBUS_RX_PIN             GPIO_P14
// #define VBUS_RX_PIN_GPIO_HZ_IN  HalGpioFmuxEnable(VBUS_RX_PIN, Bit_DISABLE)
#define VBUS_RX_PIN_UART1_RX    {HalGpioAnalogConfig(VBUS_RX_PIN, Bit_DISABLE);HalGpioFmuxConfig(VBUS_RX_PIN, FMUX_UART1_RX);}
#define VBUS_RX_PIN_AIN         {HalGpioFmuxEnable(VBUS_RX_PIN, Bit_DISABLE);HalGpioAnalogConfig(VBUS_RX_PIN, Bit_ENABLE);}

#define TEST_DP_PIN             GPIO_P10
#define TEST_DP_PIN_UART0_TX    HalGpioFmuxConfig(TEST_DP_PIN, FMUX_UART0_TX)
#define TEST_DP_PIN_UART0_RX    HalGpioFmuxConfig(TEST_DP_PIN, FMUX_UART0_RX)

#define DCIN_PIN                GPIO_P25


#define TEST_DN_PIN             GPIO_P11

#define AIN_TCHRG_PIN           GPIO_P15

#define AIN_LIGHT_PIN           GPIO_P20

#define AIN_VBAT_PIN            GPIO_P23

#define AIN_KB_PIN              GPIO_P24
#define AIN_KB_PIN_GPIO_OUT_L   {/*HalGpioAnalogConfig(AIN_KB_PIN, Bit_DISABLE);*/HalGpioPinInit(AIN_KB_PIN, GPIO_OUTPUT);HalGpioSet(AIN_KB_PIN, (bit_action_e)0);}
#define AIN_KB_PIN_AIN          {HalGpioPinInit(AIN_KB_PIN, GPIO_INPUT);HalGpioAnalogConfig(AIN_KB_PIN, Bit_ENABLE);}

#define ADC_CH_TEST_DN          ADC_CH0     // 用于检测发热丝电阻
#define ADC_CH_TEST_DN_MASK     (1 << ADC_CH_TEST_DN)

#define ADC_CH_VBAT             ADC_CH1
#define ADC_CH_VBAT_MASK        (1 << ADC_CH_VBAT)

#define ADC_CH_KB               ADC_CH2
#define ADC_CH_KB_MASK          (1 << ADC_CH_KB)

#define ADC_CH_VBUS             ADC_CH3
#define ADC_CH_VBUS_MASK        (1 << ADC_CH_VBUS)

#define ADC_CH_LIGHT            ADC_CH9
#define ADC_CH_LIGHT_MASK       (1 << ADC_CH_LIGHT)

// #define ADC_CH_EN_MASK          ((1 << ADC_CH_VBAT)|(1 << ADC_CH_KB)|(1 << ADC_CH_VBUS)|(1 << ADC_CH_LIGHT))

// 设置为1表示不启用内部分压电阻
#define ADC_CH_MODE_MASK        ((1 << ADC_CH_TEST_DN)|(1 << ADC_CH_VBAT)|(1 << ADC_CH_KB)|(1 << ADC_CH_LIGHT))

#ifndef TSS_DEBUG
#define GPIO_P02P03_INIT_LIST \
    {\
        GPIO_P02,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_PULL_UP_S      /*上拉*/\
    },\
    {\
        GPIO_P03,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_PULL_UP_S      /*上拉*/\
    },
#else
#define GPIO_P02P03_INIT_LIST
#endif

// 所有GPIO初始状态，上电首先调用
#define GPIO_INIT_LIST \
{\
    GPIO_P02P03_INIT_LIST \
    {\
        TEST_DP_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_PULL_UP_S      /*上拉*/\
    },\
    {\
        VBUS_TX_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING      /*无上下拉*/\
    },\
    {\
        SPI_SCK_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING      /*无上下拉*/\
    },\
    {\
        LCD_RS_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        TEST_DN_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)1,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        VBUS_RX_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)1,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_TCHRG_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)1,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        LCD_BR_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_LIGHT_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)1,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_VBAT_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)1,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_KB_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)1,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        DCIN_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        SPI_FLASH_CS_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        SPI_LCD_CS_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        VEXT_EN_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        VBUS_HZ_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2数mA;1:4mA.*/\
        (bit_action_e)0,  /*0：字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        SPI_MISO_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_PULL_UP_S  \
    },\
    {\
        SPI_MOSI_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    }\
}

// 所有GPIO在系统空闲时状态，关机（休眠）前首先调用
#define GPIO_IDLE_LIST \
{\
    GPIO_P02P03_INIT_LIST \
    {\
        TEST_DP_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_PULL_UP_S      /*上拉*/\
    },\
    {\
        VBUS_TX_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING      /*无上下拉*/\
    },\
    {\
        SPI_SCK_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING      /*无上下拉*/\
    },\
    {\
        LCD_RS_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        TEST_DN_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        VBUS_RX_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_TCHRG_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        LCD_BR_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_LIGHT_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_VBAT_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        AIN_KB_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        DCIN_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        SPI_FLASH_CS_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        SPI_LCD_CS_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        VEXT_EN_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)1,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        VBUS_HZ_PIN,    \
        GPIO_OUTPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2数mA;1:4mA.*/\
        (bit_action_e)0,  /*0：字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        SPI_MISO_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    },\
    {\
        SPI_MOSI_PIN,    \
        GPIO_INPUT,    /*方向*/\
        (bit_action_e)0,  /*输出电平*/\
        (bit_action_e)0,  /*0:2mA;1:4mA.*/\
        (bit_action_e)0,  /*0：数字IO；1：模拟管脚。*/\
        GPIO_FLOATING  \
    }\
}

#define KEY_ESC     0
#define KEY_UP      1
#define KEY_OK      2
#define KEY_LEFT    3
#define KEY_DOWN    4
#define KEY_RIGHT   5


#endif
